import re
import requests
from bs4 import BeautifulSoup
import pymysql
import time
from datetime import datetime
import os

if __name__ == '__main__':

    with open("/Users/yanglei/Downloads/access_api_2022_04_23_10.log", "r", encoding="utf-8") as f:
        res = f.readlines()

        db = pymysql.connect(host="localhost", port=3306, user="root", password="123456", database="test")
        for cur in res:
            ss = cur.split(" ")
            # print(cur)
            print(ss)
            ip = ss[0]
            t_time = ss[1][1:]
            t = datetime.strptime(t_time, "%d/%b/%Y:%H:%M:%S")
            method = ss[3][1:]
            if('OPTIONS' == method):
                continue
            url = ss[4]
            api = ss[4].split('?')[0]
            service = url.split('/')[1]
            status = ss[6]
            length = ss[7]
            use_time = ss[8]
            web = ss[9]
            referer = ss[10]
            client = ' '.join(ss[11:-1])
            agent = ss[-1:][0].replace('\\n','').strip()

            # 使用 cursor() 方法创建一个游标对象 cursor
            cursor = db.cursor()
            sql = "INSERT INTO `nginx_log`(`ip`, `time`, `method`, `url`, `api`, `service`, `status`, `length`, `use_time`, `web`, `referer`, `client`, `agent`) VALUES ('"+ip+"', '"+str(t)+"', '"+method+"', '"+url+"', '"+api+"', '"+service+"', '"+status+"', '"+length+"', '"+use_time+"', '"+web+"', '"+referer+"', '"+client+"', '"+agent+"')"
            cursor.execute(sql)

        db.commit()
        db.close()